package com.web.manager.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.web.manager.pojo.Teacher;
import com.web.manager.mapper.TeacherMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class TeacherService extends ServiceImpl<TeacherMapper, Teacher> {

    @Resource
    private TeacherMapper teacherMapper;

    @Override
    public boolean save(Teacher entity){
        if(StrUtil.isBlank(entity.getName())){
            entity.setName(entity.getUsername());  //默认姓名与账号一致
        }
        if(StrUtil.isBlank(entity.getPassword())){
            entity.setPassword("123");   //默认密码123
        }
        if(StrUtil.isBlank(entity.getRole())){
            entity.setRole("学生");   //默认角色
        }

        return super.save(entity);
    }


    public Teacher selectByUsername(String username){
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>(); //条件查询器
        queryWrapper.eq("username",username);  // ==> where username = #{username}


        // 根据用户名查询数据库的用户信息
        return  this.getOne(queryWrapper); // ==>select * from manager_user where username = #{username}
    }

}
